
#pragma once

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include <iostream>

using namespace cv;

namespace course_09_virtual_painter
{
    std::vector<std::vector<int>> myColor{
        {21, 22, 106, 44, 255, 255},
    };

    std::vector<Scalar> myColorValues{{255, 0, 255}};

    void findColor(Mat image)
    {
        Mat imageHSV;

        cvtColor(image, imageHSV, COLOR_BGR2HSV);
        for (int i = 0; i < myColor.size(); i++)
        {
            // 根据HSV的范围创建遮罩
            Scalar lower(myColor[i][0], myColor[i][1], myColor[i][2]);
            Scalar upper(myColor[i][3], myColor[i][4], myColor[i][5]);
            Mat mask;
            inRange(imageHSV, lower, upper, mask);
            imshow(std::to_string(i),mask);
        }
    }

    //
    void painter()
    {

        // 直接获取默认摄像头
        VideoCapture capture;
        capture.open(0, CAP_DSHOW);
        Mat image;
        // 为了持续播放需要循环  其实就是将每帧图循环放到一个画面上
        while (true)
        {
            capture.read(image);
             if (image.empty()) { std::cout<<"error img empty"; break; }

            findColor(image);

            imshow("Image", image);

            // esc 退出
            int c = waitKey(1);
            if (c == 27)
            {
                break;
            }
        }
    }

    void color_picker()
    {
        Mat image;
        int hmin = 21, smin = 22, vmin = 106;
        int hmax = 44, smax = 255, vmax = 255;
        Mat imageHSV, mask;

        // Mat image = imread(path);

        namedWindow("trackbars", (640, 200));
        createTrackbar("hmin", "trackbars", &hmin, 150);
        createTrackbar("smin", "trackbars", &smin, 150);
        createTrackbar("vmin", "trackbars", &vmin, 255);

        createTrackbar("hmax", "trackbars", &hmax, 255);
        createTrackbar("smax", "trackbars", &smax, 255);
        createTrackbar("vmax", "trackbars", &vmax, 255);

        // 直接获取默认摄像头
        VideoCapture capture;
        capture.open(0, CAP_DSHOW);

        // 为了持续播放需要循环  其实就是将每帧图循环放到一个画面上
        while (true)
        {
            capture.read(image);
            cvtColor(image, imageHSV, COLOR_BGR2HSV);

            // 根据HSV的范围创建遮罩
            Scalar lower(hmin, smin, vmin);
            Scalar upper(hmax, smax, vmax);
            inRange(imageHSV, lower, upper, mask);

            //  if (image.empty()) { break; }
            imshow("Image", image);
            imshow("IMAGE-mask", mask);

            // esc 退出
            int c = waitKey(1);
            if (c == 27)
            {
                break;
            }
        }
    }

}